<div class="container">

        <h1 id="pkg-overview">package plugin</h1>

        <p><code>import "plugin"</code>

        </p><p>
Package plugin implements loading and symbol resolution of Go plugins.
</p>
<p>
Currently plugins only work on Linux.
</p>
<p>
A plugin is a Go main package with exported functions and variables that
has been built with:
</p>
<pre>go build -buildmode=plugin
</pre>
<p>
When a plugin is first opened, the init functions of all packages not
already part of the program are called. The main function is not run.
A plugin is only initialized once, and cannot be closed.</p>


        
  


        
        

        

        

        
        
          
        

        
        


<p><a href="https://github.com/golang/go/blob/master/src/plugin/plugin.go">plugin.go</a> <a href="https://github.com/golang/go/blob/master/src/plugin/plugin_dlopen.go">plugin_dlopen.go</a> </p>

        
        

        
        

        
        
        

        
        

        
          <h2 data-kind="t" id="Plugin">type <a href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L20" title="View Source">Plugin</a>  </h2>
          <p>
Plugin is a loaded Go plugin.
</p>

          
          
          
  


          
            <h3 data-kind="f" id="Open">func <a href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L29" title="View Source">Open</a>  </h3>
            <p>
Open opens a Go plugin.
If a path has already been opened, then the existing *Plugin is returned.
It is safe for concurrent use by multiple goroutines.
</p>

            
  

          

          
            <h3 data-kind="m" id="Plugin.Lookup">func (*Plugin) <a href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L37" title="View Source">Lookup</a>  </h3>
            <p>
Lookup searches for a symbol named symName in plugin p.
A symbol is any exported variable or function.
It reports an error if the symbol is not found.
It is safe for concurrent use by multiple goroutines.
</p>

            
  

          
        
          <h2 data-kind="t" id="Symbol">type <a href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L73" title="View Source">Symbol</a>  </h2>
          <p>
A Symbol is a pointer to a variable or function.
</p>
<p>
For example, a plugin defined as
</p>
<pre>package main

// // No C code needed.
import "C"

import "fmt"

var V int

func F() { fmt.Printf("Hello, number %d\n", V) }
</pre>
<p>
may be loaded with the Open function and then the exported package
symbols V and F can be accessed
</p>
<pre>p, err := plugin.Open("plugin_name.so")
if err != nil {
	panic(err)
}
v, err := p.Lookup("V")
if err != nil {
	panic(err)
}
f, err := p.Lookup("F")
if err != nil {
	panic(err)
}
*v.(*int) = 7
f.(func())() // prints "Hello, number 7"
</pre>

<div class="clearfix" id="x-footer">
  <div class="container">
    <a href="http://studygolang.com/" target="_blank">Go语言中文网</a>
    <span class="text-muted">|</span> <a href="http://golang.org/" target="_blank">Go Language</a>
    <span class="pull-right"><a href="#">Back to top</a></span>
  </div>
</div>
<script src="../assets/jquery-2.0.3.min.js"></script>
<script src="../assets/bootstrap.min.js"></script>
<script src="../assets/site.js"></script>


</div>